package com.gzhryc.shared_device.oem.code.agent.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentRelationBusinessConfigDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelationBusinessConfig;

public class AgentRelationBusinessConfigService {
	
	static Logger log = Logger.getLogger(AgentRelationBusinessConfigService.class);

    public static AgentRelationBusinessConfigService self() {
        return MultiDBTools.getService(AgentRelationBusinessConfigService.class, true);
    }

    public static AgentRelationBusinessConfigService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentRelationBusinessConfigService.class, true);
    }

    AgentRelationBusinessConfigDao dao;

    public AgentRelationBusinessConfigService(String jdbcKey) {
        this.dao = new AgentRelationBusinessConfigDao(jdbcKey);
    }

    public List<AgentRelationBusinessConfig> findByUserKeys(Long userId,Long relationUserId){
    	if(userId != null && relationUserId != null) {
    		Conditions condition = new Conditions(AgentRelationBusinessConfig.class);
    		condition.and().is("userId", userId);
    		condition.and().is("relationUserId", relationUserId);
    		
    		try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public AgentRelationBusinessConfig getByKeys(Long userId,Long relationUserId,String businessType) {
    	if(userId != null && relationUserId != null && StringTools.isNotBlank(businessType)) {
    		Conditions condition = new Conditions(AgentRelationBusinessConfig.class);
	    	condition.and().is("userId", userId);
	    	condition.and().is("relationUserId", relationUserId);
	    	condition.and().is("businessType", businessType);
	    	
	    	try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }
    
    public boolean add(AgentRelationBusinessConfig businessConfig) throws LogicException {
    	if(businessConfig.getIncomeRatio() == null || 
    			businessConfig.getIncomeRatio() < 0 || businessConfig.getIncomeRatio() > 100) {
    		throw new LogicException("请设置分成比例在0-100以内");
    	}
    	
    	businessConfig.setCreateDate(new Date());
    	try {
			return dao.insert(businessConfig) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return false;
    }
    
    public boolean updateNotNull(AgentRelationBusinessConfig businessConfig) throws LogicException {
    	if(StringTools.isNotBlank(businessConfig.getBusinessType()) && businessConfig.getUserId() != null) {
	    	if(businessConfig.getIncomeRatio() == null || 
	    			businessConfig.getIncomeRatio() < 0 || businessConfig.getIncomeRatio() > 100) {
	    		throw new LogicException("请设置分成比例在0-100以内");
	    	}
	    	Conditions condition = new Conditions(AgentRelationBusinessConfig.class);
	    	condition.and().is("userId", businessConfig.getUserId());
	    	condition.and().is("relationUserId", businessConfig.getRelationUserId());
	    	condition.and().is("businessType", businessConfig.getBusinessType());
	    	
	    	try {
				return dao.updateNotNull(businessConfig,condition) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
    
    public boolean deleteByKeys(Long userId,Long relationUserId,String businessType) throws LogicException{
    	if(userId != null && relationUserId != null && StringTools.isNotBlank(businessType)) {
    		Conditions condition = new Conditions(AgentRelationBusinessConfig.class);
	    	condition.and().is("userId", userId);
	    	condition.and().is("relationUserId", relationUserId);
	    	condition.and().is("businessType", businessType);
	    	
	    	try {
				return dao.deleteByCondition(condition) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
    
    public boolean deleteByUsers(Long userId,Long relationUserId) {
    	if(userId != null && relationUserId != null) {
    		Conditions condition = new Conditions(AgentRelationBusinessConfig.class);
	    	condition.and().is("userId", userId);
	    	condition.and().is("relationUserId", relationUserId);
	    	
	    	try {
				return dao.deleteByCondition(condition) >= 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return false;
    }
}
